import time
from typing import List, Union

from sqlalchemy import Integer, func, or_
from sqlalchemy.orm import Session

from module_admin.entity.do.cms_car import CmsCar
from module_admin.entity.do.cms_car_team import CmsCarTeam
from module_admin.entity.do.cms_driver import CmsDriver
from module_admin.entity.vo.cms_car_team_vo import CarTeamModel, CarTeamForQuery, CarTeamForPage, CarTeamForDetailsBase, \
    CarTeamForDetails
from module_admin.entity.vo.cms_car_vo import CarSimpleModel
from module_admin.entity.vo.cms_driver_vo import DriverSimpleModel
from utils.time_format_util import object_format_datetime, list_format_datetime


class CmsCarTeamDao:
    """
    部门管理模块数据库操作层
    """

    @classmethod
    def create_car_team(cls, db: Session, page_object: CarTeamModel):
        object_dict = page_object.dict()
        cms_car_team = CmsCarTeam(**object_dict)
        db.add(cms_car_team)
        db.flush()
        return cms_car_team

    @classmethod
    def delete_car_team_by_id(cls, db: Session, id: int):
        db.query(CmsCarTeam) \
            .filter(CmsCarTeam.id == id) \
            .delete()

    @classmethod
    def get_car_team_by_carteam_id(cls, db: Session, car_team_id: str):
        return db.query(CmsCarTeam) \
            .filter(CmsCarTeam.car_team_id == car_team_id) \
            .first()

    @classmethod
    def get_car_team_by_id(cls, db: Session, id: int):
        query_for_car = db.query(CmsCarTeam.id.label("id"),
                                 CmsCarTeam.car_team_id.label("car_team_id"),
                                 CmsCarTeam.car_team_name.label("car_team_name"),
                                 CmsCarTeam.car_team_manager.label("car_team_manager"),
                                 CmsCarTeam.car_team_manager_phone.label("car_team_manager_phone"),
                                 CmsCarTeam.remark.label("remark"),
                                 CmsCarTeam.create_time.label("create_time"),
                                 CmsCarTeam.create_by.label("create_by"),
                                 CmsCarTeam.update_time.label("update_time"),
                                 CmsCarTeam.update_by.label("update_by"),
                                 CmsCar.id.label("cmsCar_id"),
                                 CmsCar.car_id.label("cmsCar_car_id"),
                                 CmsCar.car_number.label("cmsCar_car_number"),
                                 CmsCar.car_department_id.label("cmsCar_car_department_name"),
                                 CmsCar.car_type.label("cmsCar_car_type"),
                                 CmsCar.car_model.label("cmsCar_car_model"),
                                 CmsCar.car_brand.label("cmsCar_car_brand"),
                                 CmsDriver.id.label("cmsDriver_id"),
                                 CmsDriver.driver_id.label("cmsDriver_driver_id"),
                                 CmsDriver.driver_name.label("cmsDriver_driver_name"),
                                 CmsDriver.driver_model.label("cmsDriver_driver_model"),
                                 CmsDriver.phone.label("cmsDriver_phone"),
                                 ) \
            .outerjoin(CmsCar, CmsCar.car_team_id == CmsCarTeam.car_team_id) \
            .outerjoin(CmsDriver, CmsDriver.car_team_id == CmsCarTeam.car_team_id)
        filters = [CmsCarTeam.id == id]
        if filters:
            query_for_car = query_for_car.filter(*filters)
        post_list_car: List[CarTeamForDetailsBase] = list_format_datetime(
            query_for_car.all())
        post_list_car_page = []
        id: int = 0
        car_list = []
        driver_list = []
        for e in post_list_car:
            if id != e.id:
                id = e.id
                car_list = []
                driver_list = []
            car_model_for_page = CarTeamForDetails()
            car_model_for_page.id = e.id
            car_model_for_page.car_team_id = e.car_team_id
            car_model_for_page.car_team_name = e.car_team_name
            car_model_for_page.car_team_manager = e.car_team_manager
            car_model_for_page.car_team_manager_phone = e.car_team_manager_phone
            car_model_for_page.remark = e.remark
            car_model_for_page.create_time = e.create_time
            car_model_for_page.create_by = e.create_by
            car_model_for_page.update_time = e.update_time
            car_model_for_page.update_by = e.update_by
            # 车辆信息
            if e.cmsCar_id:
                car_simple_model = CarSimpleModel()
                car_simple_model.id = e.cmsCar_id
                car_simple_model.car_id = e.cmsCar_car_id
                car_simple_model.car_number = e.cmsCar_car_number
                car_simple_model.car_department_name = e.cmsCar_car_department_name
                car_simple_model.car_type = e.cmsCar_car_type
                car_simple_model.car_model = e.cmsCar_car_model
                car_simple_model.car_brand = e.cmsCar_car_brand
                car_list.append(car_simple_model)
            # 驾驶员信息
            if e.cmsDriver_id:
                driver_simple_model = DriverSimpleModel()
                driver_simple_model.id = e.cmsDriver_id
                driver_simple_model.driver_id = e.cmsDriver_driver_id
                driver_simple_model.driver_name = e.cmsDriver_driver_name
                driver_simple_model.driver_model = e.cmsDriver_driver_model
                driver_simple_model.phone = e.cmsDriver_phone
                driver_list.append(driver_simple_model)
            car_model_for_page.car_list = car_list
            car_model_for_page.driver_list = driver_list
            post_list_car_page.append(car_model_for_page)
        return post_list_car_page

    @classmethod
    def list(cls, db: Session, query_object: CarTeamForQuery):
        query_for_car = db.query(CmsCarTeam.id.label("id"),
                                 CmsCarTeam.car_team_id.label("car_team_id"),
                                 CmsCarTeam.car_team_name.label("car_team_name"),
                                 CmsCarTeam.car_team_manager.label("car_team_manager"),
                                 CmsCarTeam.car_team_manager_phone.label("car_team_manager_phone"),
                                 CmsCarTeam.remark.label("remark"),
                                 CmsCarTeam.create_time.label("create_time"),
                                 CmsCarTeam.create_by.label("create_by"),
                                 CmsCarTeam.update_time.label("update_time"),
                                 CmsCarTeam.update_by.label("update_by"),
                                 func.count(CmsCar.id).label("car_num"),
                                 func.count(CmsCar.id).label("driver_num")) \
            .outerjoin(CmsCar, CmsCar.car_team_id == CmsCarTeam.car_team_id) \
            .outerjoin(CmsDriver, CmsDriver.car_team_id == CmsCarTeam.car_team_id)
        filters = []
        if query_object.car_team_name and query_object.car_team_name.strip():
            filters.append(CmsCarTeam.car_team_name.like(f'%{query_object.car_team_name}%'))
        if filters:
            query_for_car = query_for_car.filter(*filters)
        post_list_car: List[CarTeamForPage] = list_format_datetime(
            query_for_car.group_by(CmsCarTeam.id,
                                   CmsCarTeam.car_team_id,
                                   CmsCarTeam.car_team_name,
                                   CmsCarTeam.car_team_manager,
                                   CmsCarTeam.car_team_manager_phone,
                                   CmsCarTeam.remark,
                                   CmsCarTeam.create_time,
                                   CmsCarTeam.create_by,
                                   CmsCarTeam.update_time,
                                   CmsCarTeam.update_by).all())
        post_list_car_page = []
        for e in post_list_car:
            car_model_for_page = CarTeamForPage()
            car_model_for_page.id = e.id
            car_model_for_page.car_team_id = e.car_team_id
            car_model_for_page.car_team_name = e.car_team_name
            car_model_for_page.car_team_manager = e.car_team_manager
            car_model_for_page.car_team_manager_phone = e.car_team_manager_phone
            car_model_for_page.remark = e.remark
            car_model_for_page.create_time = e.create_time
            car_model_for_page.create_by = e.create_by
            car_model_for_page.update_time = e.update_time
            car_model_for_page.update_by = e.update_by
            car_model_for_page.car_num = e.car_num
            car_model_for_page.driver_num = e.driver_num
            post_list_car_page.append(car_model_for_page)
        return post_list_car_page
